package com.huika.dealontop.control.shops.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.R;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.control.shops.adapter.SubbranchAdapter;
import com.huika.dealontop.support.domain.SubbranchBean;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.views.CommonTitleRightWindow;
import com.huika.dealontop.views.TitleBarHelper;

import java.util.ArrayList;

/**
 * @author chenyuan
 * @description：通用分店列表
 * @date 2015年1月11日 上午11:25:04
 */
public class CommonSubbranchActivity extends BaseAct {


    public static String PRODUCTID = "ProductId";
    public static String LONGITUDEX = "longitudeX";
    public static String DIMENSIONSY = "dimensionsY";

    private PullToRefreshListView ptrlv_common_subbranch;
    private SubbranchAdapter adapter;
    private TitleBarHelper titleBarHelper;
    private CommonTitleRightWindow moreWindow;
    private ImageView title_right_iv;
    private String longitudeX;
    private String dimensionsY;
    private long ProductId;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_common_subbranch);
    }

    @Override
    protected void initData() {
        super.initData();
        ProductId = getIntent().getLongExtra(PRODUCTID, 0);
        longitudeX = getIntent().getStringExtra(LONGITUDEX);
        dimensionsY = getIntent().getStringExtra(DIMENSIONSY);
        Log.d("cy", "ProductId==" + ProductId + "longitudeX==" + longitudeX + "dimensionsY==" + dimensionsY);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        titleBarHelper = new TitleBarHelper(this, R.string.common_empty, R.string.common_empty, R.string.common_subbranche_title);
        titleBarHelper.setRightPop();
//        titleBarHelper.setRightImg(new DindanDbHelper().getUnUsedableCountNum() > 0 ? R.mipmap.common_more_has_message_icon : R.mipmap.common_more_icon);
//        titleBarHelper.setOnRightImgClickListener(this);
        title_right_iv = (ImageView) findViewById(R.id.title_right_iv);

        ptrlv_common_subbranch = (PullToRefreshListView) findViewById(R.id.ptrlv_common_subbranch);
        adapter = new SubbranchAdapter(this);
        ptrlv_common_subbranch.setAdapter(adapter);
        /**测试数据*/
//        adapter.setGroup(new SubbranchBean().setDatas());

        ptrlv_common_subbranch.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                CURRENT_PAGE = 1;
                getGetProductIdMerchantInfo();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                CURRENT_PAGE++;
                getGetProductIdMerchantInfo();
            }
        });
        /**网络数据*/
        getGetProductIdMerchantInfo();
    }

    private void getGetProductIdMerchantInfo() {

        showLoadingDialog(getString(R.string.common_loading));
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
//        getUserId(ajaxParams);

        ajaxParams.putCommonTypeParam("productId", ProductId);//商品ID
        if (!TextUtils.isEmpty(longitudeX))
        ajaxParams.putCommonTypeParam("longitudeX", longitudeX);//经度
        if (!TextUtils.isEmpty(dimensionsY))
        ajaxParams.putCommonTypeParam("dimensionsY", dimensionsY);//维度

//        ajaxParams.putCommonTypeParam("productId", 9770717303L);//商品ID
//        ajaxParams.putCommonTypeParam("longitudeX", "100.253256522");//经度
//        ajaxParams.putCommonTypeParam("dimensionsY", "22.22225225");//维度


        ajaxParams.putCommonTypeParam("pagedIndex", CURRENT_PAGE+"");
        ajaxParams.putCommonTypeParam("pagedSize", PAGE_SIZE+"");

        FormResultRequest<ArrayList<SubbranchBean>> getResultRequest = new FormResultRequest<ArrayList<SubbranchBean>>(
                UrlConstants.GETPRODUCTIDMERCHANTINFO, getProductIdMerchantInfoReq, this,
                new TypeToken<RequestResult<ArrayList<SubbranchBean>>>() {
                }.getType());
        getResultRequest.setRequestParams(ajaxParams);
        executeRequest(getResultRequest);

    }

    private void getUserId(JSONWrapAjaxParams ajaxParams) {
        long u_id = GlobalApp.getInstance().getUserId();
        ajaxParams.putCommonTypeParam("userId", u_id);
    }

    private Response.Listener<RequestResult<ArrayList<SubbranchBean>>> getProductIdMerchantInfoReq = new Response.Listener<RequestResult<ArrayList<SubbranchBean>>>() {

        @Override
        public void onResponse(RequestResult<ArrayList<SubbranchBean>> result) {

            if (result.flag == 1 && result.getRs() != null && result.getRs().size() > 0) {
                ptrlv_common_subbranch.setVisibility(View.VISIBLE);
                if (CURRENT_PAGE == 1) adapter.setGroup(result.getRs());
                else adapter.addItems(result.getRs());
                changeRefreshMode(result.getTotalSize());
            } else {
                ptrlv_common_subbranch.setVisibility(View.GONE);
            }
            dismissLoadingDialog();
            ptrlv_common_subbranch.onRefreshComplete();
        }
    };


    private void changeRefreshMode(int totalSize) {
        if (totalSize == 0) {
            ptrlv_common_subbranch.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        } else if (adapter.getCount() >= totalSize) {
            ptrlv_common_subbranch.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        } else {
            ptrlv_common_subbranch.setMode(PullToRefreshBase.Mode.BOTH);
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub
        super.onErrorResponse(error);
        dismissLoadingDialog();
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.title_right_iv:
                moreWindow = new CommonTitleRightWindow(aty);
                moreWindow.showWindow(title_right_iv);
                break;
        }

    }
}
